package cn.zy._05tx;

import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 实例化消息生产者Producer
        TransactionMQProducer mqProducer = new TransactionMQProducer("hello-producer-group");
        // 设置NameServer的地址
        mqProducer.setNamesrvAddr("127.0.0.1:9876");
        //指定事务监听器
        mqProducer.setTransactionListener(new MyTransactionCheckListener());
        // 启动Producer实例
        mqProducer.start();

        // 创建消息1，并指定Topic，Tag和消息体
        Message message = new Message("hello-topic5", "hello-tags5", "杨冰怡".getBytes(RemotingHelper.DEFAULT_CHARSET));
        TransactionSendResult transactionSendResult = mqProducer.sendMessageInTransaction(message, "假面骑士钢斗");
        System.out.println(transactionSendResult);

        // 如果不再发送消息，关闭Producer实例
        mqProducer.shutdown();
    }
}